//
//  DBCenter.m
//  OurRelaxDemo
//
//  Created by apple on 15/6/30.
//  Copyright (c) 2015年 于宾尧. All rights reserved.
//

#import "DBCenter.h"

@implementation DBCenter
+ (DBCenter *)shareInstence
{
    static DBCenter *dbHandler = nil;
    if (dbHandler == nil) {
        dbHandler = [[DBCenter alloc]init];
    }
    return dbHandler;
}
- (void)judgeResult:(int)result type:(NSString *)type
{
    if (result == SQLITE_OK) {
        NSLog(@"%@成功",type);
    }else
    {
        NSLog(@"%@失败,失败编号: %d",type ,result);
    }
}
- (void)openDB
{
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dbPath = [docPath stringByAppendingPathComponent:@"shuju.db"];
    NSLog(@"%@", dbPath);
    
    
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
    [self judgeResult:result type:@"打开数据库"];
}
- (void)closeDB
{
    int result = sqlite3_close(dbPoint);
    [self judgeResult:result type:@"关闭数据库"];
    
}
- (void)createTable
{
    NSString *sqlStr = @"create table song (singer_name text primary key, song_name text , url_list text)";
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], NULL, NULL, NULL);
    [self judgeResult:result type:@"创建表"];
}
- (int)insertModel:(DataModel *)data
{
   
    NSString *sqlStr = [NSString stringWithFormat:@"insert into song values('%@', '%@', '%@')", data.singer, data.name, data.url];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], NULL, NULL, NULL);
    [self judgeResult:result type:@"插入歌曲"];
//    NSLog(@"??????????%d", result);
    return result;
    
}
- (void)deleteAll
{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from song"];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], NULL, NULL, NULL);
    [self judgeResult:result type:@"删除所有"];
    
}
- (void)delete:(DataModel *)song
{
    
    NSString *sqlStr = [NSString stringWithFormat:@"delete from song where song_name = '%@'", song.name];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], NULL, NULL, NULL);
    [self judgeResult:result type:@"删除"];
}
- (NSMutableArray *)selectAll
{
    NSString *sqlStr = [NSString stringWithFormat:@"select * from song"];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare(dbPoint, [sqlStr UTF8String], -1, &stmt, NULL);
    NSMutableArray *songArr = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            DataModel *song = [[DataModel alloc]init];
            
            const unsigned char *name = sqlite3_column_text(stmt, 0);
            song.singer = [NSString stringWithUTF8String:(const char *)name];
            const unsigned char *songName = sqlite3_column_text(stmt, 1);
            song.name = [NSString stringWithUTF8String:(const char *)songName];
            const unsigned char *urlStr = sqlite3_column_text(stmt, 2);
            song.url = [NSString stringWithUTF8String:(const char *)urlStr];
            
            [songArr addObject:song];
            [song release];
            
        }
    }
    sqlite3_finalize(stmt);
    return songArr;
    
}
@end
